﻿using ExampleService.Infrastructure;
using ExampleService.Xxxxx.SDK.Contract;
using Microsoft.Extensions.DependencyInjection;
using WebApiClientCore.Extensions.OAuths;
using WebApiClientCore.Extensions.OAuths.TokenProviders;

namespace ExampleService.Xxxxx.SDK
{
    public class AuthTokenProvider : TokenProvider
    {
        public AuthTokenProvider(IServiceProvider serviceProvider) : base(serviceProvider)
        {
        }

        protected override async Task<TokenResult> RequestTokenAsync(IServiceProvider serviceProvider)
        {
            LoginRequest login = new LoginRequest
            {
                UserName = "admin",
                Password = "123456"
            };

            // 一般情况下，是要 appid 和 appsecret 的，这里只是为了演示
            var res = await serviceProvider.GetRequiredService<IOAuthContract>().GetToken(login);

            return new TokenResult
            {
                Access_token = res.Token
            };
        }

        protected override Task<TokenResult> RefreshTokenAsync(IServiceProvider serviceProvider, string refresh_token)
        {
            return this.RequestTokenAsync(serviceProvider);
        }
    }
}
